package com.servlet;

import com.dao.CateGoryDao;
import com.dao.Impl.CateGoryDaoImp;
import com.dao.Impl.UnitDaoImpl;
import com.dao.UnitDao;
import com.entity.Category;
import com.entity.Unit;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet(value = "/unitController/*")
public class UnitServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String uri = request.getRequestURI();
        String method = uri.substring(uri.lastIndexOf("/") + 1);
        String target = "";
        switch (method) {
            case "add":
                target = add(request, response);
                break;
            case "show":
                target = show(request, response);
                break;
            case "modify":
                target = modify(request, response);
                break;
            case "findId":
                target = findId(request, response);
                break;
            case "delete":
                target = delete(request, response);
                break;
            case "pageQuery":
                target = pageQuery(request, response);
                break;
            default:
                target = show(request, response);
        }
        request.getRequestDispatcher(target).forward(request, response);
    }
    /**
     * 添加商品分类
     *
     * @param request
     * @param response
     * @return
     */
    private String add(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String[]> map = request.getParameterMap();
        Unit unit = new Unit();
        try {
            BeanUtils.populate(unit, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        UnitDao dao = new UnitDaoImpl();
        dao.add(unit);
        return show(request,response);
    }

    /**
     * 查询全部商品类别
     *
     * @param request
     * @param response
     * @return
     */
    private String show(HttpServletRequest request, HttpServletResponse response) {
        UnitDao dao = new UnitDaoImpl();
        List<Unit> list = dao.findAll();
        request.setAttribute("list", list);
        return "/shop/shopManage.jsp";
    }

    /**
     * 修改数据
     * @param request
     * @param response
     * @return
     */
    private String modify(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String[]> map = request.getParameterMap();
        Unit unit = new Unit();
        try {
            BeanUtils.populate(unit, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        UnitDao dao = new UnitDaoImpl();
        dao.modify(unit);
        return show(request,response);
    }

    /**
     * 根据id查询
     * @param request
     * @param response
     * @return
     */
    private String findId(HttpServletRequest request, HttpServletResponse response) {
        Integer id = Integer.valueOf(request.getParameter("id"));
        UnitDao dao = new UnitDaoImpl();
        Unit unit = dao.findById(id);
        request.setAttribute("unit", unit);
        return "/shop/shopManage.jsp";
    }

    /**
     * 删除数据
     * @param request
     * @param response
     * @return
     */
    private String delete(HttpServletRequest request, HttpServletResponse response) {
        UnitDao dao = new UnitDaoImpl();
        Integer id = Integer.valueOf(request.getParameter("id"));
        dao.del(id);
        return show(request,response);
    }

    private String pageQuery(HttpServletRequest request, HttpServletResponse response) {
        UnitDao dao = new UnitDaoImpl();
        Integer count = Integer.valueOf(request.getParameter("count"));
        List<Unit> li = dao.findSctn(1, count,"desc");
        request.setAttribute("list", li);
        request.setAttribute("count",count);
        return "/shop/shopManage.jsp";

    }
}
